//index.js
const app = getApp();
import api from "../../../utils/util";

Page({
  data: {
    ent: app.globalData.ent,
    statusBarHeight: app.globalData.statusBarHeight,
    fullHeight: app.globalData.fullcontain,
    discount: app.globalData.discount,
    navmess:{
      navtitle: "EG伊果（日本）",
      backhide: 1,
      topheight: 1
    },
    curIdx: 0,
    swiperSetting:{
      indicatorDots: true,
      vertical: false,
      autoplay: false,
      interval: 2000,
      duration: 500,
    },
    webHref: "",
    // 最新活动
    actlist: []
  },
  pageto: function(e){
    var url = e.currentTarget.dataset.url,
        data = e.currentTarget.dataset.datobj;
    app.goto(url,data);
  },
  toAppointDetail: function(e){
    var url = e.currentTarget.dataset.url,
        mess = e.currentTarget.dataset.mess;
    wx.navigateTo({
      url: url +"?sign="+ mess +"&type="+ 0,
    })
  },
  onLoad: function(options) {
    wx.hideTabBar();
    // 刷新组件
    this.refreshView = this.selectComponent("#refreshView")
    // 数据请求
    api.post("/mini/home/banner", {
      'ent': this.data.ent
    }).then(res => {
        console.log("banner：",res.result);
        this.setData({
          bannerlist: res.result
        })
    }).catch(err => {
        console.log(err)
    });
    api.post("/mini/home/recommendStaff",{
      'ent': this.data.ent
    }).then(res => {
        console.log("明星美容师：",res.result);
        this.setData({
          starlist: res.result
        })
    }).catch(err => {
        console.log(err)
    });
    api.post("/mini/home/recommendProject",{
      "ent": this.data.ent,
      "limit": 4
    }).then(res => {
        console.log("美容项目：",res.result);
        this.setData({
          beautyprolist: res.result
        })
    }).catch(err => {
        console.log(err)
    });
    api.post("/mini/home/recommendProduct",{
      "ent": this.data.ent,
      "limit": 4
    }).then(res => {
        console.log("精品推荐：",res.result);
        this.setData({
          goodslist: res.result
        })
    }).catch(err => {
        console.log(err)
    });
    // 最新活动
    api.post("/mini/home/recommendActivities",{
      "ent": this.data.ent
    }).then(res => {
        res.result.spellGroupMiNi.imageUrl = JSON.parse(res.result.spellGroupMiNi.imageUrl);
        res.result.bargainMiNi.imageUrl = JSON.parse(res.result.bargainMiNi.imageUrl);
        res.result.scoreMiNi.imageUrl = JSON.parse(res.result.scoreMiNi.imageUrl);
        res.result.seckillMiNi.imageUrl = JSON.parse(res.result.seckillMiNi.imageUrl);
        this.setData({
          actlist: res.result
        })
        console.log("最新活动：",res.result);
    }).catch(err => {
        console.log(err)
    });
    
  },
  onShow: function() {
    wx.hideTabBar();
    var token = wx.getStorageSync('token'),
        check = app.checkLogin();
    if(check){
      this.setData({
        isLogin: check,
        token: token
      })
    }
  },
  banerclick: function(e){
    this.setData({
      webHref: e.currentTarget.dataset.imgIndex
    })
  },
  tojf: function(e){
    if(this.data.isLogin){
      wx.navigateTo({
        url: e.currentTarget.dataset.url + "?mess=" + JSON.stringify(e.currentTarget.dataset.mess),
      })
    }else{
      wx.navigateTo({
        url: "../../login/login",
      })
    }
  },
   // 详情页跳转
  takegoodto: function(e){
    if(this.data.isLogin){
      wx.navigateTo({
        url: e.currentTarget.dataset.url + "?sign=" + JSON.stringify(e.currentTarget.dataset.sign),
      })
    }else{
      wx.navigateTo({
        url: "../../login/login",
      })
    }
  },
  // 最新活动详情页
  actToDetail: function(e){
    if(this.data.isLogin){
      wx.navigateTo({
        url: e.currentTarget.dataset.url + "?mess=" + JSON.stringify(e.currentTarget.dataset.activityno),
      })
    }else{
      wx.navigateTo({
        url: "../../login/login",
      })
    }
  },
  // 点击免费拿
  newbarfun: function(e){
    var that = this;
    if(this.data.isLogin){
      console.log("新建砍价",e.target.dataset.obj);
      that.setData({
        newbargain: true,
        toshareobj: e.target.dataset.obj
      })
    }else{
      wx.navigateTo({
        url: '../../login/login',
      })
    }
  },
  closebarfn: function(e){
    var that = this;
    console.log("砍价窗口返回数据",e.detail); //子组件返回参数
    that.setData({
      newbargain: false,
    })
  },
  // 优惠券领取
  receiveopen: function(e){
    var that = this;
    console.log('优惠券sign：',e.currentTarget.dataset.sign)
    if(that.data.isLogin){
      api.post("/mini/ext/coupon/doReceive",{
        "token": that.data.token,
        "sign": e.currentTarget.dataset.sign
      }).then(res => {
        console.log(res)
        if(res.code == 200){
          that.setData({
            receivestate: true
          })
        }else{
          wx.showToast({
            title: '领取失败',
            icon: "none",
            duration: 1500
          })
        }
      })
    }else{
      wx.navigateTo({
        url: '../../login/login',
      })
    }
  },
  closereceive: function(){
    this.setData({
      receivestate: false
    })
  },
})
